'use strict';
var dayjs = require('dayjs')
var duration = require('dayjs/plugin/duration')
dayjs.extend(duration)
module.exports = {
    /**
     * XXXnameXXX
     * @url client/booths/kh/getBoothsInfo 前端调用的url参数地址
     * data 请求参数
     * @param {String} params1  参数1
     */
    main: async (event) => {
        let {
            data = {}, userInfo, util, filterResponse, originalParam
        } = event;
        let {
            customUtil,
            uniID,
            config,
            pubFun,
            vk,
            db,
            _
        } = util;
        let {
            uid
        } = data;
        let res = {
            code: 0,
            msg: ""
        };
        var $ = _.aggregate; // 聚合查询操作符
        let time = Date.now();
        let dbName = "bidding-biddingprices";
        res = await vk.baseDao.getTableData({
            dbName,
            data,
        // return await vk.baseDao.selects({
        //     dbName: "bidding-biddingprices", // 主表名

            whereJson: {
                userid: uid,
                 status: _.neq(-1)
            },
            sortArr: [{
                "name": "_add_time",
                "type": "desc"
            }],
            groupJson: {
                _id: "$booth_id", 
                booth_id: $.first("$booth_id"),
                price: $.max("$price"),
                _add_time: $.max("$_add_time") 
            },
            foreignDB: [{
                dbName: "bidding-booths", 
                localKey: "booth_id", 
                foreignKey: "_id", 
                as: "boothinfo",
                whereJson: {
                    // endtime: _.gte(time),//大于现在,
                    _id:_.exists(true)
                },
                limit: 1,
                foreignDB: [
                    {
                        dbName: "bidding-biddingprices",
                        localKey: "_id",
                        foreignKey: "booth_id",
                        as: "biddingList",
                        whereJson: {
                            // endtime: _.gte(time),//大于现在,
                            status: _.neq(-1)
                        
                        },
                        limit: 2,
                        sortArr: [{ "name": "price", "type": "desc" }],
                    },
                ]
            }
            ],
            lastWhereJson:{
                "boothinfo._id" : _.exists(true)
              }
        });
        return res;
        // let booth = await vk.baseDao.selects({
        // 	dbName: "bidding-booths", // 主表名

        // 	whereJson: {
        // 		_id: data._id,
        // 	},
        // 	// 副表列表
        // 	foreignDB: [{
        // 		dbName: "bidding-biddingprices", // 副表名
        // 		localKey: "_id", // 主表外键字段名
        // 		foreignKey: "booth_id", // 副表外键字段名
        // 		as: "biddingList",
        // 		limit: 10, // 当limit = 1时，以对象形式返回，否则以数组形式返回
        // 		whereJson: { // 条件
        // 			userid:uniID
        // 		},
        // 		// 副表字段显示规则
        // 		fieldJson: {},
        // 		// 副表排序规则
        // 		sortArr: [{
        // 			"name": "_add_time",
        // 			"type": "desc"
        // 		}],

        // 	},
        // 	]
        // });

    }
}